package com.story.modular.app.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.constant.factory.PageFactory;
import com.story.common.persistence.model.Search;
import com.story.core.base.controller.BaseController;
import com.story.core.log.LogObjectHolder;
import com.story.modular.app.dao.SearchMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import com.story.modular.app.service.SearchService;

/**
 * @param
 * @author pyx
 * @date 2019/12/27 14:54
 * @return
 */
@Controller
@RequestMapping("search")
public class SearchController extends BaseController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private SearchMapper searchMapper;
    private static String PREFIX = "/system/keyword/";

    @RequestMapping("index")
    public String index() {
        return PREFIX + "keyword.html";
    }

    @RequestMapping("/search_add")
    public String keywordAdd() {
        return PREFIX + "keyword_add.html";
    }

    @RequestMapping("/search_edit/{searchId}")
    public String deptUpdate(@PathVariable Integer searchId, Model model) {
        Search dict = searchMapper.findById(searchId);
        model.addAttribute("dict", dict);
        LogObjectHolder.me().set(dict);
        return PREFIX + "keyword_edit.html";
    }

    /**
     * 得到关键词列表
     *
     * @param
     * @return
     */
    @ApiOperation("得到关键词列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(String keyword) {
        Page<Search> page = new PageFactory<Search>().defaultPage();
        if (keyword == null || "".equals(keyword)) {
            keyword = null;
        }
        //type = 1表示为视频资源
        List<Map<String, Object>> result = searchMapper.getSearchList(page, keyword);
        return result;
    }

    /**
     * 删除关键词
     *
     * @param
     * @return [keyword]
     * @author pyx
     * @date 2019/12/27 16:19
     */
    @ApiOperation("删除关键词")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object search(String searchId) {
        List<Integer> IdList = new ArrayList<Integer>();
        String[] IdArr = searchId.split(",");
        for (String Id : IdArr) {
            searchMapper.delete(Integer.valueOf(Id));
        }
        return SUCCESS_TIP;
    }

    /**
     * 刷新
     *
     * @param
     * @return [searchId]
     * @author pyx
     * @date 2019/12/27 16:28
     */
    @ApiOperation("刷新关键词")
    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    @ResponseBody
    public Object refresh(String searchId) {
        List<Integer> IdList = new ArrayList<Integer>();
        String[] IdArr = searchId.split(",");
        for (String Id : IdArr) {
            List<Search> searches = Collections.singletonList(searchMapper.findById(Integer.valueOf(Id)));
            searches.forEach(e -> {
                String keyword = e.getKeyword();
                e.setVideoAlbum(searchMapper.findVideoCate(keyword).size());
                e.setVideoContent(searchMapper.findVideo(keyword).size());
                e.setMusicAlbum(searchMapper.findMusicCate(keyword).size());
                e.setMusicContent(searchMapper.findMusic(keyword).size());
                searchMapper.update(e);
            });
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改人气
     *
     * @param id, po
     * @return [id, po]
     * @author pyx
     * @date 2019/12/27 17:51
     */
    @ApiOperation("修改人气")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(Integer id, String keyword, Integer po) {
        Search search = searchMapper.findById(id);
        search.setPopularity(po);
        search.setKeyword(keyword);
        search.setLastTime(new Date());
        searchMapper.update(search);
        return SUCCESS_TIP;
    }

    /**
     * 添加关键词
     *
     * @param keyword
     * @return [id, keyword]
     * @author pyx
     * @date 2019/12/27 17:51
     */
    @ApiOperation("添加关键词")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Object insert(String keyword, Integer po) {
        Integer count = searchMapper.findKeyword(keyword);
        if (count == 0) {
            Search search = new Search();
            search.setKeyword(keyword);
            search.setPopularity(po);
            search.setVideoAlbum(searchMapper.findVideoCate(keyword).size());
            search.setVideoContent(searchMapper.findVideo(keyword).size());
            search.setMusicAlbum(searchMapper.findMusicCate(keyword).size());
            search.setMusicContent(searchMapper.findMusic(keyword).size());
            search.setCreateTime(new Date());
            search.setLastTime(new Date());
            searchMapper.add(search);
            return SUCCESS_TIP;
        } else {
            return SUCCESS_TIP;
        }
    }

    /**
     * app搜索
     *
     * @param
     * @return
     * @author pyx
     * @date 2019/12/30 9:48
     */
    @ApiOperation("app搜索 参数keyword type 1为音频专辑 2为音频 3为视频专辑 4为视频")
    @RequestMapping(value = "/appSearch", method = RequestMethod.POST)
    @ResponseBody
    public Object appSearch(@ApiParam(name = "keyword", value = "关键词", required = true) String keyword,
                            @ApiParam(name = "type", value = "类型 1为音频专辑 2为音频 3为视频专辑 4为视频", required = true) String type) {
//        String keyword = super.getPara("keyword").trim();
//        String type = super.getPara("type").trim();
        Integer count = searchMapper.findKeyword(keyword);
        List<Map<String, Object>> returnValue = null;
        if (count == 0) {
            Search search = new Search();
            returnValue = searchMapper.findMusicCate(keyword);
            search.setMusicAlbum(returnValue.size());
            returnValue = searchMapper.findMusic(keyword);
            search.setMusicContent(returnValue.size());
            returnValue = searchMapper.findVideoCate(keyword);
            search.setVideoAlbum(returnValue.size());
            returnValue = searchMapper.findVideo(keyword);
            search.setVideoContent(returnValue.size());
            returnValue = getMaps(keyword, type);
            search.setKeyword(keyword);
            search.setPopularity(1);
            search.setCreateTime(new Date());
            search.setLastTime(new Date());
            searchMapper.add(search);
        } else {
            returnValue = getMaps(keyword, type);
            searchMapper.updateByKeyword(keyword);
        }
        return returnValue;
    }

    private List<Map<String, Object>> getMaps(String keyword, String type) {
        List<Map<String, Object>> returnValue;
        if ("1".equals(type)) {
            //音频专辑
            returnValue = searchMapper.findMusicCate(keyword);
        } else if ("2".equals(type)) {
            //音频
            returnValue = searchMapper.findMusic(keyword);
        } else if ("3".equals(type)) {
            //视频专辑
            returnValue = searchMapper.findVideoCate(keyword);
        } else if ("4".equals(type)) {
            //视频
            returnValue = searchMapper.findVideo(keyword);
        } else {
            returnValue = null;
        }
        return returnValue;
    }


    /**
     * e.点击先应的专辑、音频、视频，热度+1；
     *
     * @param
     * @return
     * @author pyx
     * @date 2019/12/30 11:00
     */
    @ApiOperation("点击先应的专辑、音频、视频，热度+1； 参数为id，type  type为1 专辑 type为2表示视频或音频")
    @RequestMapping(value = "/rateAddOne", method = RequestMethod.POST)
    @ResponseBody
    public Object rateAddOne(@ApiParam(name = "id", value = "专辑或视频音频id", required = true) String id,
                             @ApiParam(name = "type", value = "类型  type为1 专辑 type为2表示视频或音频", required = true) String type) {
//        String id = super.getPara("id").trim();
//        String type = super.getPara("type").trim();
        if (("1").equals(type)) {
            //专辑
            searchMapper.updateZhuanJiByKeyword(id);
        } else if (("2").equals(type)) {
            //视频音乐
            searchMapper.updateOtherByKeyword(id);
        } else {
            return "参数错误";
        }
        return SUCCESS_TIP;
    }

    /**
     * top 30
     *
     * @param
     * @return
     * @author pyx
     * @date 2019/12/30 11:43
     */
    @ApiOperation("app top 30")
    @RequestMapping(value = "/appTop", method = RequestMethod.POST)
    @ResponseBody
    public Object appTop30() {
        return searchMapper.selectTop30();
    }

    /**
     * app
     *
     * @param
     * @return []
     * @author pyx
     * @date 2019/12/30 11:44
     */
    @ApiOperation("app视频专辑的榜单 参数type 1为音频专辑 2为音频 3为视频专辑 4为视频")
    @RequestMapping(value = "/appTopSon", method = RequestMethod.POST)
    @ResponseBody
    public Object appTopSon(@ApiParam(name = "type", value = "类型  1为音频专辑 2为音频 3为视频专辑 4为视频", required = true) String type) {
//        String type = super.getPara("type").trim();
        List<Map<String, Object>> returnValue;
        if ("1".equals(type)) {
            //音频专辑
            returnValue = searchMapper.findMusicCate("");
        } else if ("2".equals(type)) {
            //音频
            returnValue = searchMapper.findMusic("");
        } else if ("3".equals(type)) {
            //视频专辑
            returnValue = searchMapper.findVideoCate("");
        } else if ("4".equals(type)) {
            //视频
            returnValue = searchMapper.findVideo("");
        } else {
            returnValue = null;
        }
        return returnValue;
    }
}
